package com.libinshen.wxpay.videoAliBaBa.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.libinshen.wxpay.common.vo.CommonResult;
import com.libinshen.wxpay.videoAliBaBa.entity.form.CourseInfoForm;
import com.libinshen.wxpay.videoAliBaBa.entity.vo.CoursePublishVo;
import com.libinshen.wxpay.videoAliBaBa.entity.vo.CourseQueryVo;
import com.libinshen.wxpay.videoAliBaBa.entity.vo.CourseVo;
import com.libinshen.wxpay.videoAliBaBa.service.CourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author lbs
 */
@CrossOrigin
@Api("课程管理")
@RequestMapping("/edu/course")
@Slf4j
public class CourseController {

    @Resource
    private CourseService courseService;

    @ApiOperation("保存课程信息")
    @PostMapping("save-course-info")
    public CommonResult saveCourseInfo(
            @ApiParam(name = "CourseInfoForm", value = "课程基本信息", required = true)
            @RequestBody CourseInfoForm courseInfoForm) {
        if (courseInfoForm == null) {
            return CommonResult.ok().data("item", "请求数据不能为空");
        }

        String courseId = courseService.saveCourseInfo(courseInfoForm);
        if (courseId == null) {
            return CommonResult.error().data("item", "保存失败");
        }

        return CommonResult.ok().data("item", courseId);
    }

    @ApiOperation("根据ID查询课程信息")
    @GetMapping("course-info/{id}")
    public CommonResult getById(
            @ApiParam(name = "课程ID", value = "课程基本信息", required = true)
            @PathVariable String id) {
        if (StringUtils.isEmpty(id)) {
            return CommonResult.ok().data("item", "id不能为空");
        }
        CourseInfoForm courseInfoForm = courseService.getCourseInfoById(id);

        if (courseInfoForm == null) {
            return CommonResult.ok().data("item", "请求数据不存在");
        }
        return CommonResult.ok().data("item", courseInfoForm);
    }

    @ApiOperation("更新课程")
    @PutMapping("update-course-info")
    public CommonResult updateCourseInfoById(
            @ApiParam(name = "CourseInfoForm", value = "课程基本信息", required = true)
            @RequestBody CourseInfoForm courseInfoForm) {
        if (courseInfoForm == null) {
            return CommonResult.error().setMessage("数据不能为空");
        }

        courseService.updateCourseInfoById(courseInfoForm);
        return CommonResult.ok().setMessage("数据修改成功");
    }

    @ApiOperation("课程分页列表{注意：教师的分页查询是单表的分页查询，课程的分页查询是多表的分页查询}")
    @GetMapping("list/{page}/{limit}")
    public CommonResult listPage(@ApiParam(value = "当前页码", required = true) @PathVariable Long page,
                                 @ApiParam(value = "每页记录数", required = true) @PathVariable Long limit,
                                 @ApiParam("课程列表查询对象") CourseQueryVo courseQueryVo) {
        IPage<CourseVo> pageModel = courseService.selectPage(page, limit, courseQueryVo);
        List<CourseVo> records = pageModel.getRecords();
        long total = pageModel.getTotal();
        return CommonResult.ok().data("total", total).data("rows", records);
    }

    @ApiOperation("根据ID删除课程")
    @DeleteMapping("remove/{id}")
    public CommonResult removeById(
            @ApiParam(value = "课程ID", required = true)
            @PathVariable("id") String id) {
        if (!StringUtils.isEmpty(id)) {
            return CommonResult.error().setMessage("課程id不能为空");
        }

        boolean isDeleted = courseService.deleteCourseInfoById(id);
        if (!isDeleted) {
            return CommonResult.error().setMessage("課程删除失败");
        }
        return CommonResult.ok().setMessage("数据删除成功");
    }

    @ApiOperation(value = "根据ID获取课程发布信息")
    @GetMapping("course-publish-info/{id}")
    public CommonResult getCoursePublishVoById(
            @ApiParam(name = "id", value = "课程ID", required = true)
            @PathVariable String id) {

        CoursePublishVo courseInfoForm = courseService.getCoursePublishVoById(id);
        if (courseInfoForm == null) {
            return CommonResult.error().setMessage("数据不存在");
        }
        return CommonResult.ok().data("item", courseInfoForm);
    }

    @ApiOperation(value = "根据id发布课程")
    @PutMapping("publish-course/{id}")
    public CommonResult publishCourseById(
            @ApiParam(name = "id", value = "课程ID", required = true)
            @PathVariable String id) {

        boolean isPublished = courseService.publishCourseById(id);
        if (isPublished) {
            return CommonResult.error().setMessage("課程发布失败");
        }
        return CommonResult.ok();
    }
}
